package com.szxy.poitl;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.style.TableStyle;
import com.szxy.domain.AuthorData;
import com.szxy.plugin.AuthorPluginTablePolicy;
import com.szxy.poitl.domain.Author;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;

import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: zwz
 * @Date:2020/7/24 18:04
 * @Description:com.szxy.poitl
 * @Version:1.0 poi-tl（poi template language）是基于Apache POI的Word模板引擎，纯Java组件，跨平台，
 * 代码短小精悍，通过插件机制使其具有高度扩展性。
 **/
//@Log4j2
public class PoitlMain {

    /**
     * 文件名
     */
    public static final String FILE_NAME = "template.docx";
    /**
     * 文件完整路径
     */
    public static final String path = PoitlMain.class.getClassLoader().getResource(FILE_NAME).getPath();

    /**
     * 数据
     */
    public static AuthorData datas = new AuthorData();

    /**
     * 行样式
     */
    public static TableStyle rowStyle = new TableStyle();


    public static void beforeWord() {
        rowStyle.setAlign(STJc.CENTER);

        datas.setTitle("Hello POI-TL 真好用！");
        datas.setContent("有趣的 POI-TL！ 哈哈哈哈哈哈哈哈哈哈或或或或或或或或或或或或或或或或或或或或或或或或或或或或");
        datas.setAuthor(new Author("小方", 210, "男"));
        RowRenderData author1 = new RowRenderData().build("1", "小方", "20", "男");
        RowRenderData author2 = new RowRenderData().build("2", "小方2", "48", "男");
        RowRenderData author3 = new RowRenderData().build("3", "小方3", "34", "男");
        author1.setStyle(rowStyle);
        author2.setStyle(rowStyle);
        author3.setStyle(rowStyle);
        List<RowRenderData> authorList = Arrays.asList(author1, author2, author3);
        datas.setAuthorList(authorList);
    }


    public static void authorExample() throws Exception {
        // 声明配置
        Configure config = Configure.newBuilder().customPolicy("author_table", new AuthorPluginTablePolicy()).build();
        // 核心 API
        XWPFTemplate template = XWPFTemplate.compile(path, config).render(datas);
        // 输出
        FileOutputStream out = new FileOutputStream("D:\\desktop\\out_author.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }

    /**
     * 测试
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        beforeWord();
        authorExample();
    }


}
